package it.webbased.site.server;

import it.webbased.site.client.ContactData;
import it.webbased.site.client.ContactService;

import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.logging.Logger;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class ContactServiceImpl extends RemoteServiceServlet implements
		ContactService {
	
	private static final Logger log = Logger.getLogger(ContactServiceImpl.class.getName());
	
	@Override
	public String addContact(ContactData data) {
		log.info(data.toString());
		String err = data.validate(true);
		if(!err.isEmpty()) {
			return "Parametri errati: "+err;
		}
		
		Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

        String msgBodyInfo = "Campi del form:\n"+data.toString();
        String msgBodyClient = "<p><b>Messaggio inviato Correttamente</b></p>" +
		"<p>Gentile <b>"+ data.get("Nome").getValue()+" "+data.get("Cognome").getValue()+"</b>,<br/>" +
		"verrete contattati quanto prima dal nostro personale.</p>" +
		"<p>Grazie, <b>WebBased.it</b></p>";

 

        String subj = new String("Richiesta informazioni dal form");
        
        try {
            InternetAddress from;
			from = new InternetAddress("mail@webbased.it", "WebBased.it - Mail Sender");
	        InternetAddress[] replTo = new InternetAddress[1];
	        replTo[0]= new InternetAddress("info@webbased.it","WebBased.it - Info");
            Message msgInfo = new MimeMessage(session);
            msgInfo.setFrom(from);
            msgInfo.setReplyTo(replTo);
            msgInfo.addRecipient(Message.RecipientType.TO,
                             new InternetAddress("info@webbased.it", "WebBased.it - Info"));
            msgInfo.setSubject("Richiesta informazioni dal form");
            msgInfo.setText(msgBodyInfo);
            Multipart mp = new MimeMultipart();
            MimeBodyPart htmlPart = new MimeBodyPart();
            htmlPart.setContent(msgBodyInfo, "text/html");
            mp.addBodyPart(htmlPart);
            msgInfo.setContent(mp);
            Transport.send(msgInfo);
            
            Message msgClient = new MimeMessage(session);
            msgClient.setFrom(from);
            msgClient.setReplyTo(replTo);
            msgClient.setSubject(subj);
            msgClient.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(data.get("E-mail").getValue(),
                    		data.get("Nome").getValue()+" "+data.get("Cognome").getValue()));
            mp.removeBodyPart(htmlPart);
            htmlPart.setContent(msgBodyClient, "text/html");
            mp.addBodyPart(htmlPart);
            msgClient.setContent(mp);
            Transport.send(msgClient);

        } catch (AddressException e) {
            // ...
			e.printStackTrace();
			return "Errore imprevisto";
        } catch (MessagingException e) {
            // ...
			e.printStackTrace();
			return "Errore imprevisto";
        } catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "Errore imprevisto";
		}
		
		return msgBodyClient;
	}

}
